import axios from "axios"
import qs from 'qs'
import router from '../router'
import { Toast } from 'vant';
var ToastBox ="";
function floatMul(a, b) {
    var c = 0,
        d = a.toString(),
        e = b.toString();
    try {
        c += d.split(".")[1].length;
    } catch (f) {}
    try {
        c += e.split(".")[1].length;
    } catch (f) {}
    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}
const common  ={
    install:function(Vue){
        Vue.prototype.getUrl = function (name){
            if(window.location.search) { //判断是否hash模式
                var parameter = window.location.search;
            } else {
                var parameter = "?" + window.location.hash.slice(2).split("?")[1];
        
            }
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = parameter.substr(1).match(reg);
            if(r != null) {
                return decodeURI(r[2]);
            } else {
                return null;
            }
		},
		Vue.prototype.jumPage = function (url){
			router.push(url);
		},
		Vue.prototype.commonConfig = {
			errorImg:  "this.src='./images/backimg.png'",
			dontBack:{//不支持银行卡列表
				     BOCO:"交通银行",
				     ECITIC:"中信银行",
					 CEB:"光大银行",
					 PINGAN:"平安银行",
					 CMBCHINA:"招商银行",
					 CCB:"中国建设银行",
					 ICBC:"中国工商银行",
					 CMBC:"民生银行",
					 SPDB:"浦东发展银行",
					 CGB:"广发银行",
					 HXB:"华夏银行",
					 BOC:"中国银行",
					 POST:"邮储银行",
					 SHB:"上海银行",
					 BCCB:"北京银行"
			}
		},
		Vue.prototype.NumberStr =(str)=> { //处理数字前面的0
			return str.replace(/\b(0+)/gi,"")
		},
        Vue.prototype.$axiosFun = function (url,data,type){
			clearTimeout(ToastBox);
			ToastBox = setTimeout(()=>{
                Toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                    duration: 0
                });
            },300)
			return new Promise((resolve,reject)=>{ 
				var href = this.$store.state.host+url; //传递的参数
				var postData = "";
				axios({
					url:href,
					method:type,
					params: type === 'get' ? data : null,
					data: type === 'post' ? qs.stringify(data) : null,
					// data: type === 'post' ? data: null,
					// headers: {
					// 	'token':token
					//   },
				}).then(res=>{
					clearTimeout(ToastBox);
					Toast.clear();
					if(res.data.code==-1){
						Toast("Token 已过期")
					}
					resolve(res.data);
					
				}).catch(res =>{
					clearTimeout(ToastBox);
					Toast.clear();
					Toast("服务器错误")
					resolve(res.data);
				})
			})
	
		},
		Vue.prototype.$getAxios = function (url,data,type){
			clearTimeout(ToastBox);
			ToastBox = setTimeout(()=>{
                Toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                    duration: 0
                });
            },300)
			return new Promise((resolve,reject)=>{ 
				var postData = "";
				axios({
					url:url,
					method:type,
					params: type === 'get' ? data : null,
					data: type === 'post' ? qs.stringify(data) : null,
					// data: type === 'post' ? data: null,
					// headers: {
					// 	'token':token
					//   },
				}).then(res=>{
					clearTimeout(ToastBox);
					Toast.clear();
					if(res.data.code==-1){
						Toast("Token 已过期")
					}
					resolve(res.data);
					
				}).catch(res =>{
					clearTimeout(ToastBox);
					Toast.clear();
					Toast("服务器错误")
					resolve(res.data);
				})
			})
	
		},
		Vue.prototype.$accAdd = function (a, b){ //精准加法
			var c, d, e;
			if(undefined==a||null==a||""==a||isNaN(a)){a=0;}
			if(undefined==b||null==b||""==b||isNaN(b)){b=0;}
			try {
				c = a.toString().split(".")[1].length;
			} catch (f) {
				c = 0;
			}
			try {
				d = b.toString().split(".")[1].length;
			} catch (f) {
				d = 0;
			}
			e = Math.pow(10, Math.max(c, d));
			return  (floatMul(a, e) + floatMul(b, e)) / e;
		},
		Vue.prototype.$fmthkdate = function (statementDate,repaymentDate){ //计算距离账单日有多长时间 statementDate:账单日 repaymentDate：还款日
			var myDate = new Date().getDate();
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var d = new Date(year, month, 0);
			var s = d.getDate();
			// console.log('本月'+s)
			// console.log('今天'+myDate)
			//statementDate:账单日  repaymentDate:还款日 myDate:当前是几号 s:这个月有几天
			
			if (parseInt(statementDate) < parseInt(repaymentDate)) {

				if(statementDate<myDate&&repaymentDate>myDate){ //当前日期 再 还款日与当前日期之前
					var datt = parseInt(repaymentDate) - parseInt(myDate)
				}else{
					var datt = parseInt(repaymentDate) - parseInt(statementDate)
				}
				return datt
			} else if(parseInt(statementDate) > parseInt(repaymentDate)){
				if(statementDate > myDate && repaymentDate > myDate) {
					return repaymentDate * 1 - myDate;
				} else {
					return s * 1 - statementDate * 1 + repaymentDate * 1
				}
			}
		},
		Vue.prototype.$accMul = function (arg1,arg2){ //精准乘法
			var m = 0, 
			s1 = arg1.toString(), 
			s2 = arg2.toString();
			try {
				m += s1.split(".")[1].length
			} catch (e) {
			}
			try {
				m += s2.split(".")[1].length
			} catch (e) {
			}
			return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
	
		},
		Vue.prototype.$PrefixInteger = function (num, length){ //位数补零
			return (Array(length).join('0') + num).slice(-length);
		}

    }
}
export default common